package com.yugao.fintech.antelope.system.service.impl;

import com.yugao.fintech.antelope.base.model.enums.ConfigDefineEnum;
import com.yugao.fintech.antelope.base.model.enums.ConfigKeyEnum;
import com.yugao.fintech.antelope.base.model.module.auth.LoginUser;
import com.yugao.fintech.antelope.base.model.module.config.ConfigInfo;
import com.yugao.fintech.antelope.system.SystemApi;
import com.yugao.fintech.antelope.system.model.entity.SysUser;
import com.yugao.fintech.antelope.system.model.rqrs.accesskey.AccessKeyResp;
import com.yugao.fintech.antelope.system.model.rqrs.config.ConfigGetReq;
import com.yugao.fintech.antelope.system.model.rqrs.config.ConfigResp;
import com.yugao.fintech.antelope.system.model.rqrs.log.LoginUserGetReq;
import com.yugao.fintech.antelope.system.model.rqrs.log.OperateLogSaveReq;
import com.yugao.fintech.antelope.system.model.rqrs.tenant.TenantSaveReq;
import com.yugao.fintech.antelope.system.model.rqrs.tenant.TenantUpdateReq;
import com.yugao.fintech.antelope.system.model.rqrs.user.LoginUserDeleteReq;
import com.yugao.fintech.antelope.system.model.rqrs.user.UserPageReq;
import com.yugao.fintech.antelope.system.model.rqrs.user.UserResp;
import com.yugao.fintech.antelope.system.model.rqrs.user.UserSaveReq;
import com.yugao.fintech.antelope.system.service.*;
import com.yugao.fintech.framework.validation.ValidationUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Primary
@Service
@RequiredArgsConstructor
public class SystemApiImpl implements SystemApi {
    private final AccessKeyService accessKeyService;
    private final SysOperateLogService operateLogService;
    private final SysUserService userService;
    private final SysConfigService configService;
    private final AuthorizeService authorizeService;
    private final SysTenantUserService tenantUserService;
    private final SysTenantService tenantService;

    @Override
    public AccessKeyResp getAccessKeyBySecretId(String secretId) {
        return accessKeyService.getAccessKeyBySecretId(secretId);
    }

    @Override
    public AccessKeyResp getAccessKeyById(Long id) {
        return accessKeyService.getAccessKeyById(id, null);
    }

    @Override
    public void saveOperateLog(OperateLogSaveReq req) {
        operateLogService.save(req);
    }

    @Override
    public LoginUser getLoginUser(LoginUserGetReq req){
        return userService.getLoginUser(req);
    }

    @Override
    public Long saveUser(UserSaveReq req) {
        ValidationUtils.validateFast(req);
        return userService.saveUser(req);
    }

    @Override
    public List<UserResp> listUser(UserPageReq query) {
        return userService.list(query);
    }

    @Override
    public List<UserResp> listUserByIds(List<Long> userIds) {
        return userService.listUserByIds(userIds);
    }

    @Override
    public void removeUserById(Long userId) {
        userService.deleteUserByIds(Collections.singletonList(userId));
    }

    @Override
    public UserResp getUserById(Long initUserId) {
        return userService.getUserById(initUserId);
    }

    @Override
    public void deleteLoginUser(LoginUserDeleteReq req) {
        ValidationUtils.validateFast(req);
        userService.deleteLogicUser(req);
    }

    @Override
    public Long saveTenant(TenantSaveReq req) {
        return tenantService.saveTenant(req);
    }

    @Override
    public void deleteTenant(Long tenantId) {
        tenantService.deleteTenant(tenantId);
    }

    @Override
    public void updateTenant(TenantUpdateReq req) {
        tenantService.updateTenant(req);
    }

    @Override
    public ConfigResp getConfig(ConfigKeyEnum configKey) {
        return configService.getConfig(new ConfigGetReq(configKey.getKey()));
    }

    @Override
    public ConfigInfo getUseConfig(ConfigDefineEnum configDefine) {
        return configService.getUseConfig(configDefine);
    }

    @Override
    public Map<String, List<String>> listAssignRoleUrl(List<Long> roleIds) {
        return authorizeService.listAssignRoleUrl(roleIds);
    }

    @Override
    public SysUser getUserByAccount(String account) {
        return userService.getUserByAccount(account);
    }
}
